package com.example.appjava;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.Parenthesis;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.ItemsList;
import net.sf.jsqlparser.expression.operators.relational.MultiExpressionList;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.Statements;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.*;
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.statement.values.ValuesStatement;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author sjw
 * @className Sql
 * @date 2021/12/10 8:07
 * @description: TODO
 */
public class Sql1 {
    private static final String sql = "SELECT r.id id,r.item_id itemId,c.customer_code customerCode,r.people_id " +
            "FROM reporting r " +
            "LEFT JOIN (SELECT customer_id,reporting_id FROM reporting_customer_relation WHERE isdelete=FALSE AND reporting_id IN (1)) rcr " +
            "ON r.id=rcr.reporting_id " +
            "LEFT JOIN customer c " +
            "ON rcr.customer_id=c.id " +
            "WHERE r.isdelete=FALSE " +
            "AND r.item_id<> 1 " +
            "AND customer_code<>'' " +
            "AND r.id IN (1) ";

    public static void main(String[] args) throws JSQLParserException {
//        Statement stmt = CCJSqlParserUtil.parse(sql);
        Statement stmt = CCJSqlParserUtil.parse("select * from a,b");
        if (stmt instanceof Select) parse((Select) stmt);
        System.out.println( stmt.toString());
//		 SpringApplication.run(PlatformHouseHiddenCallMiniappApplication.class, args);
    }

    private static void parse(Select select) {
        SelectBody selectBody = select.getSelectBody();
        parse(selectBody);
    }

    private static void parse(SelectBody selectBody) {
        if (selectBody instanceof PlainSelect) parse((PlainSelect) selectBody);
    }

    private static void parse(SubSelect subSelect) {
        SelectBody selectBody = subSelect.getSelectBody();
        parse(selectBody);
    }

    private static void parse(PlainSelect plainSelect) {
        FromItem fromItem = plainSelect.getFromItem();
        whereFill(fromItem,plainSelect);
        List<Join> joins = plainSelect.getJoins();
        if (!CollectionUtils.isEmpty(joins)) joins.forEach(join -> whereFill(join,plainSelect));
    }

    private static void whereFill(Join join, PlainSelect plainSelect) {
        FromItem fromItem = join.getRightItem();
        whereFill(fromItem,plainSelect);
    }

    private static void whereFill(FromItem fromItem, PlainSelect plainSelect) {
        if (fromItem instanceof Table) whereFill((Table) fromItem,plainSelect);
        if (fromItem instanceof SubSelect) parse((SubSelect) fromItem);
    }

    private static void whereFill(Table table, PlainSelect plainSelect) {
        Column column = new Column(table.getAlias() == null ? null : table,"tenant_id");
        LongValue value = new LongValue(1);
        EqualsTo equalsTo = new EqualsTo();
        equalsTo.setLeftExpression(column);
        equalsTo.setRightExpression(value);
        Expression where = plainSelect.getWhere();
        Expression newWhere = new AndExpression(new Parenthesis(where),equalsTo);
        plainSelect.setWhere(newWhere);
    }
}